package com.api.shopapi.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.api.shopapi.entity.*;
import com.api.shopapi.entity.dto.ActivityOrderDTO;
import com.api.shopapi.entity.dto.ActivityOrderRecordDTO;
import com.api.shopapi.service.ActivityOrderRecordImgService;
import com.api.shopapi.service.ActivityOrderRecordService;
import com.api.shopapi.service.ActivityOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * 活动的拍照记录
 * */
@RestController
@RequestMapping("ActivityOrderRecord")
public class ActivityOrderRecordController extends BaseController{

    @Autowired
    @Qualifier("ActivityOrderRecordServiceImplOne")
    private ActivityOrderRecordService activityOrderRecordService;

    @Autowired
    @Qualifier("ActivityOrderRecordImgServiceImplOne")
    private ActivityOrderRecordImgService activityOrderRecordImgService;


    //志愿队伍拍照上传
    @RequestMapping(value = "Add", method = RequestMethod.POST)
    public MyResult Add(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");

            String activityOrderCode = jsonObject.getString("activityOrderCode");

            JSONArray arr = jsonObject.getJSONArray("arr");
            String remarks = jsonObject.getString("remarks");

            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/ActivityOrderRecord/Add");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }


            ActivityOrderRecord activityOrderRecord =new ActivityOrderRecord();
            activityOrderRecord.setActivityOrderRecordCode(UUID.randomUUID().toString());
            activityOrderRecord.setActivityOrderCode(activityOrderCode);
            activityOrderRecord.setCreateTime(new Date());
            activityOrderRecord.setText(remarks);
            activityOrderRecordService.insert(activityOrderRecord);

            for(int i=0;i<arr.size();i++){
                JSONObject jsonObject1 = arr.getJSONObject(i);

                ActivityOrderRecordImg activityOrderRecordImg = new ActivityOrderRecordImg();
                activityOrderRecordImg.setActivityOrderRecordImgCode(UUID.randomUUID().toString());
                activityOrderRecordImg.setActivityOrderRecordCode(activityOrderRecord.getActivityOrderRecordCode());
                activityOrderRecordImg.setImgPath(jsonObject1.getString("imgPath"));
                activityOrderRecordImg.setSort(jsonObject.getInteger("sort"));
                activityOrderRecordImgService.insert(activityOrderRecordImg);

            }


            JSONObject jsonObject1 =new JSONObject();
            return success(jsonObject1);
        } catch (Exception e) {
            insertError("/ActivityOrderRecord/Add",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }
    }



    /**
     * 拍照记录详情
     * 小程序
     * */
    @RequestMapping(value = "Get",method = RequestMethod.POST)
    public MyResult Get(@RequestBody JSONObject jsonObject){
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");

            String activityOrderCode = jsonObject.getString("activityOrderCode");

            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/ActivityOrderRecord/Get");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            List<ActivityOrderRecord> listActivityOrderRecord = activityOrderRecordService.ListByActivityOrderCode(activityOrderCode);

            List<ActivityOrderRecordDTO> listDTO = new ArrayList<>();
            for(ActivityOrderRecord activityOrderRecord:listActivityOrderRecord){

                ActivityOrderRecordDTO activityOrderRecordDTO = mapper.map(activityOrderRecord,ActivityOrderRecordDTO.class);

                List<ActivityOrderRecordImg> listImg = activityOrderRecordImgService.ListByActivityOrderRecordCode(activityOrderRecord.getActivityOrderRecordCode());
                activityOrderRecordDTO.setListActivityOrderRecordImg(listImg);
                listDTO.add(activityOrderRecordDTO);

            }


            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("list",listDTO);
            return success(jsonObject1);
        } catch (Exception e) {
            insertError("/ActivityOrderRecord/Get", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }



}
